using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using BTreeSharp;

namespace TheTest
{
    struct TypeLen
    {
        public TypeLen(Type t,int len)
        {
            this.t = t;
            this.len = len;
        }
       
        public Type Type
        {
            get { return t; }
        }
        public int Length
        {
            get { return len; }
        }
	    
        Type t;
        public int len;
    }
    [TestFixture]
    public class TestByteConverter
    {
        [Test]
        public void TestTypesAvailability()
        {
            TypeLen[] types = new TypeLen[] { 
                                new TypeLen(typeof(byte),1)
                                ,new TypeLen(typeof(char),2)
                                ,new TypeLen(typeof(short),2)
                                ,new TypeLen(typeof(int),4)
                                ,new TypeLen(typeof(float),4)
                                ,new TypeLen(typeof(long),8)
                                ,new TypeLen(typeof(double),8)
                                ,new TypeLen(typeof(DateTime),8)
                                ,new TypeLen(typeof(Decimal),16) //??
                                ,new TypeLen(typeof(UInt16),2)
                                ,new TypeLen(typeof(UInt32),4)
                                ,new TypeLen(typeof(UInt64),8)
                                ,new TypeLen(typeof(string),8)
                                ,new TypeLen(typeof(byte[]),8)
                                };
            BuiltInByteConverter bc = new BuiltInByteConverter();
            int howManyBytes;
            for (int i = 0; i < types.Length; ++i) 
            {
                Assert.IsTrue(bc.CanConvert(types[i].Type, out howManyBytes),"Test fail on type:"+types[i].Type.ToString());
                Assert.AreEqual(types[i].Length, howManyBytes,"Test fail on type:"+types[i].Type.ToString());
            }

        }
        [Test]
        public void PersistSimpleTypes()
        {
            BuiltInByteConverter bc = new BuiltInByteConverter();
            Assert.AreEqual((byte)5, bc.Rehydrate(bc.Convert((byte)5),typeof(byte)));
            Assert.AreEqual('a', bc.Rehydrate(bc.Convert('a'), typeof(char)));
            Assert.AreEqual((short)-5, bc.Rehydrate(bc.Convert((short)-5), typeof(short)));
            Assert.AreEqual((int)-5, bc.Rehydrate(bc.Convert((int)-5), typeof(int)));
            Assert.AreEqual(5.0f, bc.Rehydrate(bc.Convert(5.0F), typeof(float)));
            Assert.AreEqual((long)-5, bc.Rehydrate(bc.Convert((long)-5), typeof(long)));
            Assert.AreEqual((double)5, bc.Rehydrate(bc.Convert((double)5), typeof(double)));
            DateTime dt = DateTime.Now;
            Assert.AreEqual(dt, bc.Rehydrate(bc.Convert(dt), typeof(DateTime)));
            Assert.AreEqual(5M, bc.Rehydrate(bc.Convert(5M), typeof(decimal)));
            Assert.AreEqual((UInt16)0xFFFF, bc.Rehydrate(bc.Convert((UInt16)0xFFFF), typeof(UInt16)));
            Assert.AreEqual((UInt32)0xFFFFFFFF, bc.Rehydrate(bc.Convert((UInt32)0xFFFFFFFF), typeof(UInt32)));
            Assert.AreEqual((UInt64)0xFFFFFFFFFFFFFFFF, bc.Rehydrate(bc.Convert((UInt64)0xFFFFFFFFFFFFFFFF), typeof(UInt64)));
        }
    }
}
